﻿namespace Hidistro.UI.AccountCenter.CodeBehind
{
    using Hidistro.Core;
    using Hidistro.Membership.Context;
    using Hidistro.Messages;
    using Hidistro.UI.Common.Controls;
    using System;
    using System.Web.UI.HtmlControls;
    using System.Web.UI.WebControls;
    using Hidistro.Entities.Orders;
    using Hidistro.Entities.Sales;
    using Hidistro.SaleSystem.Member;
    using System.Collections.Specialized;
    using System.Web.UI;
    using wnxd.javascript;
    using System.Web;
    public class UserCenter : MemberTemplatedWebControl
    {
        private DropDownList dropPayType;
        private IButton btnPay;
        private HtmlInputHidden hdorderId;
        protected override void AttachChildControls()
        {
            this.btnPay = ButtonManager.Create(this.FindControl("btnPay"));
            this.btnPay.Click += new EventHandler(this.btnPay_Click);
            this.hdorderId = (HtmlInputHidden)this.FindControl("hdorderId");
            this.dropPayType = (DropDownList)this.FindControl("dropPayType");

            PageTitle.AddSiteNameTitle("用户中心");
            if (!this.Page.IsPostBack)
            {
                Member user = Users.GetUser(HiContext.Current.User.UserId, false) as Member;
                //if (!user.IsOpenBalance)
                //{
                //    this.LkUpdateTradePassword.Visible = false;
                //}
                this.dropPayType.DataSource = TradeHelper.GetPaymentModes(PayApplicationType.payOnPC);
                this.dropPayType.DataTextField = "Name";
                this.dropPayType.DataValueField = "ModeId";
                this.dropPayType.DataBind();
            }
        }
        private void btnPay_Click(object sender, EventArgs e)
        {
            string orderId = this.hdorderId.Value;
            int result = 0;
            int.TryParse(this.dropPayType.SelectedValue, out result);
            PaymentModeInfo paymentMode = TradeHelper.GetPaymentMode(result);
            if (paymentMode != null)
            {
                OrderInfo orderInfo = TradeHelper.GetOrderInfo(orderId);
                orderInfo.PaymentTypeId = paymentMode.ModeId;
                orderInfo.Gateway = paymentMode.Gateway;
                orderInfo.PaymentType = paymentMode.Name;
                TradeHelper.UpdateOrderPaymentType(orderInfo);
            }
            this.Page.Response.Redirect(Globals.GetSiteUrls().UrlData.FormatUrl("sendPayment", new object[] { orderId }));
        }

        protected json btnPays()
        {
            string orderId = this.Page.Request.Form["orderid"];
            int result = 0;
            int.TryParse(this.Page.Request.Form["type"], out result);
            PaymentModeInfo paymentMode = TradeHelper.GetPaymentMode(result);
            if (paymentMode != null)
            {
                OrderInfo orderInfo = TradeHelper.GetOrderInfo(orderId);
                orderInfo.PaymentTypeId = paymentMode.ModeId;
                orderInfo.Gateway = paymentMode.Gateway;
                orderInfo.PaymentType = paymentMode.Name;
                TradeHelper.UpdateOrderPaymentType(orderInfo);
            }
            return new json(new { url = Globals.GetSiteUrls().UrlData.FormatUrl("sendPayment", new object[] { orderId }) });
        }
        protected override void OnInit(EventArgs e)
        {
            // this.Page.Response.Redirect("http://account.x-juse.com/user/UserCenter.aspx?sdomain=" + HttpUtility.UrlEncode("http://" + this.Page.Request.Url.Authority));
            if (NginxServer.IsInit)
            {
            }
            else
                NginxServer.Init();


            string strAccountserverName = NginxServer.GetAccountServerName();
            string strNgxinServerName = NginxServer.GetNginxServerName();
            string strredir;

            if (string.IsNullOrEmpty(strAccountserverName))
            { 
                if(string.IsNullOrEmpty(strNgxinServerName))
                { 
                    strredir = "http://account.x-juse.com//user/UserCenter.aspx?sdomain=" + HttpUtility.UrlEncode("http://" + this.Page.Request.Url.Authority);
                }
                else
                {
                    strredir = "http://account.x-juse.com//user/UserCenter.aspx?sdomain=" + HttpUtility.UrlEncode("http://" + strNgxinServerName);
                }

            }
            else
            {
                if (string.IsNullOrEmpty(strNgxinServerName))
                {
                    strredir = "http://" + strAccountserverName + "/user/UserCenter.aspx?sdomain=" + HttpUtility.UrlEncode("http://" + this.Page.Request.Url.Authority);
                }
                else
                {
                    strredir = "http://" + strAccountserverName + "/user/UserCenter.aspx?sdomain=" + HttpUtility.UrlEncode("http://" + strNgxinServerName);
                }
            }

            this.Page.Response.Redirect(strredir);

            if (this.SkinName == null)
            {
                this.SkinName = "User/Skin-UserCenter.html";
            }
            base.OnInit(e);
        }
       
    }
}
